<?php

//$doc = JFactory::getDocument();
//$doc->addScript('components/com_popup/public/js/jquery.js');

$usertype = $this->usertype;
$mailgroup = $this->mailgroup;
$area = $this->area;
$pref = $this->pref;
$rows = $this->rows;
?>
<div class="popup">
   <form action="<?php echo JRoute::_('index.php?option=com_questionare&tmpl=component');?>" name="adminForm" id="adminForm" class="form-popup recipients" method="post">
      <!-- filter -->
      <div id="filter">
         <div class="row">
            <p class="col">
               <label>ユーザタイプ</label>
               <select name="usertype" id="usertype" class="medium">
               <!-- <option value="all" >全ユーザ種別</option> -->
               <option value="office" >本部ユーザ</option>
               <option value="center" >センターユーザ</option>
               <option value="club" >クラブユーザ</option>        
               </select>              
            </p>

         </div>
         <div class="row">
            <p class="col">
               <label>地区</label>
               <select name="area" id="area" class="medium">
                  <option value="0" selected="selected">全て</option>
                  <?php
                    $area_search = $this->area_search;
                    foreach($area as $value){
                  ?>
                  <option value="<?php echo $value->AREA_CODE; ?>" <?php $temp = isset($area_search) ? $area_search : false;if ($temp == $value->AREA_CODE) { echo ' selected="selected"';} ?> >
                  <?php echo $value->AREA_NAME; ?></option>
                  <?php
                    }
                  ?>
               </select>
            </p>
            <p class="col">
               <label>都道府県</label>
               <span id="area_pref">
                   <select name="pref" id="pref" class="medium">
                      <option value="0" selected="selected">全て</option>
                      <?php 
                        $pref_search = $this->pref_search;              
                        foreach($pref as $value){                       
                      ?>
                      <option value="<?php echo $value->PREF_CODE; ?>" <?php $temp = isset($pref_search) ? $pref_search : false;if ($temp == $value->PREF_CODE) { echo ' selected="selected"';} ?> ><?php echo $value->PREF_NAME; ?></option>
                      <?php      
                        }                 
                      ?>
                   </select>
               </span>
            </p>
         </div>
         <div class="row">
            <p class="col">
               <label>センター・クラブ名</label>
               <input class="big" type="text" name="ocba_name" id="ocba_name" value="<?php echo $this->ocba_name; ?>" />
            </p>
            <p class="col right" style='vertical-align:top'>
               <button type="button" id="btnfind"><?php echo JText::_('検索'); ?></button>
	             <button type="button" id="btnclear" ><?php echo JText::_('クリア'); ?></button>                
            </p>
         </div>
      </div>
      <!-- end filter -->
      <!-- table -->
      <div class="clearfix"></div>
      <div id="the_wrap">
          <div class="the_scroll">
              <table>
                 <thead>
                    <tr>
                        <th class="check"><input id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                        <th class="code">配信先コード </th>
                        <th class="mail">配信先名</th>
                    </tr>
                 </thead>
                 
                 <tbody>
                 <?php
                 if($rows != null){
                    foreach($rows as $value){
                        if($value->u_id != null){
                 ?>
                    <tr>
                       <td class="center">
                            <input class="checkbox-id" type="checkbox" id="cb<?php echo $value->u_id; ?>" name="cid[]" value="<?php echo $value->u_id; ?>" onclick="isChecked(this.checked);checkID();" />                           
                       </td>
                       <td id="user<?php echo $value->u_id; ?>"><?php echo $value->name; ?></td>
                       <td id="email<?php echo $value->u_id; ?>"><?php echo $value->email; ?></td>
                    </tr>
                 <?php
                        }
                    }
                 }else{
                 ?>
                    <tr>
                        <td class="center" colspan="3">データは見つかりませんでした</td>
                    </tr>
                 <?php
                    }            
                 ?>  
                 </tbody>         
              </table>         
          </div>
          <input type="hidden" name="task" value="" />
          <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
          <textarea name="list_uid" id="list_uid"></textarea>
      </div>
      <!--<a href="#" onclick="if (document.adminForm.boxchecked.value==0){alert('まず、リストから選択してください。');}">Check</a>-->
      
   </form>
</div>

<script>
    function checkAll(a,b){b||(b="cb");if(a.form){for(var c=0,d=0,f=a.form.elements.length;d<f;d++){var e=a.form.elements[d];if(e.type==a.type&&(b&&0==e.id.indexOf(b)||!b))e.checked=a.checked,c+=!0==e.checked?1:0}if(a.form.boxchecked)a.form.boxchecked.value=c;return!0}for(var e=document.adminForm,c=e.toggle.checked,f=a,g=0,d=0;d<f;d++){var h=e[b+""+d];if(h)h.checked=c,g++}document.adminForm.boxchecked.value=c?g:0}
    function isChecked(a,b){"undefined"===typeof b&&(b=document.getElementById("adminForm"));!0==a?b.boxchecked.value++:b.boxchecked.value--};   
	jq(document).ready(function(){
       
		jq("#area").change(function(){
		  
			var id = document.getElementById("area").value;
			var url = "index.php?option=com_questionare&task=selectArea&area_code="+id;
            
            jq.get(url, function(data) {
              jq('#area_pref').html(data);
            });
            	
		})
        
        //find
        jq("#btnfind").click(function(){
                jq.ajax({
                  url: 'index.php?option=com_questionare&task=search_user'
                    +'&usertype_search='+jq('#usertype option:selected').val()
                    +'&area_search='+jq('#area option:selected').val()
                    +'&pref_search='+jq('#pref option:selected').val()
                    +'&ocba_name='+jq('#ocba_name').val(),
                  context: document.body
                }).done(function(data ) {
                  jq('#the_wrap').html(data);
                });
        })
        //clear
        jq("#btnclear").click(function(){
            var usertype_search = 0;
            var mailgroup_search = 0;
            var area_search = 0;
            var pref_search = 0;
            var ocba_name = 0;
            jq("#usertype").val(0);
            jq("#mailgroup").val(0);
            jq("#area").val(0);
            jq("#pref").val(0);
            
            jq("#ocba_name").val(null);
            jq("#the_wrap").html(
                '<div class="the_scroll">'+
                  '<table>'+
                     '<thead>'+
                        '<tr>'+
                            '<th class="check"><input id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>'+
                            '<th class="code">配信先コード</th>'+
                            '<th class="mail">配信先名</th>'+
                        '</tr>'+
                     '</thead>'+                   
                     '<tbody>'+
                        '<tr>'+
                            '<td class="center" colspan="3">データは見つかりませんでした</td>'+
                        '</tr>'+                   
                     '</tbody>'+      
                  '</table>'+         
              '</div>'+
              '<input type="hidden" name="task" value="" />'+
              '<input type="hidden" name="boxchecked" id="boxchecked" value="0"/>'+
              '<textarea name="list_uid" id="list_uid"></textarea>'
            );
              
        })
        
                			
	});
 
    function checkID(){
      
        var str = '';
        jq( '.checkbox-id' ).each(function() {
            if(jq(this).attr('checked')){
                id = jq(this).val();
                str += jq('#email'+id).html()+'('+jq('#user'+id).html()+');';
            }
        });
        
        // set hidden text
        jq('#list_uid').html(str);

    }
    
</script>
<?php 

$app = &JFactory::getApplication();
$app->close();

?>